/* -------------------------------------------------------------------------- *
   TestWinUnit - Maria Blees (maria.blees@microsoft.com)

   File: AssertExceptionTest.cpp

   This file is meant for testing the AssertException classes, specifically
   the AppendMessage methods.
 * -------------------------------------------------------------------------- */
#include "WinUnit.h"

// Note the use of an unnamed namespace to hold local classes, functions and
// data.
namespace
{
    // This class is an AssertExceptionW with a 5-character buffer which we can
    // view the MessageLength of.
    class InspectableAssertExceptionWFive : public WinUnit::AssertExceptionW<5>
    {
    public:
        size_t GetMessageLength() { return this->MessageLength; }
    };

    // This class is an AssertExceptionA with a 5-character buffer which we can
    // view the MessageLength of.
    class InspectableAssertExceptionAFive : public WinUnit::AssertExceptionA<5>
    {
    public:
        size_t GetMessageLength() { return this->MessageLength; }
    };
}


BEGIN_TEST(AssertExceptionWAppendMessageNMinus1Chars)
{
    InspectableAssertExceptionWFive exception;
    exception.AppendMessage(L"good");
    
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());

    // Appending more should have no effect
    exception.AppendMessage(L"hello");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionWAppendMessageWithNChars)
{
    InspectableAssertExceptionWFive exception;
    exception.AppendMessage(L"goods");

    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());

    // Appending more should have no effect
    exception.AppendMessage(L"hello");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionWAppendMessageTooLong)
{
    InspectableAssertExceptionWFive exception;
    exception.AppendMessage(L"no");
    WIN_ASSERT_EQUAL(2U, exception.GetMessageLength());

    WIN_ASSERT_STRING_EQUAL(L"no", exception.Message());

    exception.AppendMessage(L"good");
    WIN_ASSERT_STRING_EQUAL(L"nogo", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionWAppendEmptyString)
{
    InspectableAssertExceptionWFive exception;

    exception.AppendMessage(L"");
    WIN_ASSERT_STRING_EQUAL(L"", exception.Message());
    WIN_ASSERT_ZERO(exception.GetMessageLength());

    exception.AppendMessage(L"goods");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());

    exception.AppendMessage(L"");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionAAppendMessageNMinus1Chars)
{
    InspectableAssertExceptionAFive exception;
    exception.AppendMessage("good");
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
    
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());

    // Appending more should have no effect
    exception.AppendMessage("hello");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionAAppendMessageWithNChars)
{
    InspectableAssertExceptionAFive exception;
    exception.AppendMessage("goods");

    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());

    // Appending more should have no effect
    exception.AppendMessage("hello");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionAAppendMessageTooLong)
{
    InspectableAssertExceptionAFive exception;
    exception.AppendMessage("no");
    WIN_ASSERT_EQUAL(2U, exception.GetMessageLength());

    WIN_ASSERT_STRING_EQUAL(L"no", exception.Message());

    exception.AppendMessage("good");
    WIN_ASSERT_STRING_EQUAL(L"nogo", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST

BEGIN_TEST(AssertExceptionAAppendEmptyString)
{
    InspectableAssertExceptionAFive exception;

    exception.AppendMessage("");
    WIN_ASSERT_STRING_EQUAL(L"", exception.Message());
    WIN_ASSERT_ZERO(exception.GetMessageLength());

    exception.AppendMessage("goods");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());

    exception.AppendMessage("");
    WIN_ASSERT_STRING_EQUAL(L"good", exception.Message());
    WIN_ASSERT_EQUAL(4U, exception.GetMessageLength());
}
END_TEST